package com.zlzvip.android.app;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.zlzvip.android.R;
import com.zlzvip.android.app.storagemanager.SpDao;

import butterknife.Bind;


public class MySplashActivity extends BaseActivity {
    Handler handler = new Handler();
    @Bind(R.id.iv_top)
    ImageView mIvTop;
    Runnable runnable;
    @Bind(R.id.stvBottomTime)
    TextView mStvBottomTime;
    private boolean mIsFirstStart;
    private MyCountDownTimer mc;
    Intent intent;
    private String url;
    private String h5Link;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        intent = new Intent();
        url = getIntent().getStringExtra("url");
        h5Link = getIntent().getStringExtra("h5Link");
        hideToolbar();
        initData();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
//            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        mIsFirstStart = SpDao.isFirstTimeStartApp(this);
    }


    private void initData() {
        Glide.with(MySplashActivity.this).load(url).into(mIvTop);
        mIvTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TopPicActivity.start(MySplashActivity.this,h5Link);
                handler.removeCallbacks(runnable);
            }
        });
        initActivity();
    }

    private void initActivity() {
        mc = new MyCountDownTimer(4000, 1000);
        mc.start();
        runnable = new Runnable() {
            public void run() {
                if (mIsFirstStart) {
                    intent.setClass(MySplashActivity.this, MyGuideActivity.class);
                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                    startActivity(intent);
                    finish();
                    SpDao.setKeyFirstTimeStartApp(MySplashActivity.this, false);
                } else {
                    intent.setClass(MySplashActivity.this, MainActivity.class);
                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                    startActivity(intent);
                    finish();
                }
            }
        };
        handler.postDelayed(runnable, 4000);
    }

    /**
     * 隐藏标题栏， 不论是custom的还是toolbar
     */
    public void hideToolbar() {
        View customToolbar = getCustomToolbar();
        if (customToolbar != null && customToolbar.getVisibility() == View.VISIBLE) {
            customToolbar.setVisibility(View.GONE);
        }

        if (getToolbar() != null && getToolbar().getVisibility() == View.VISIBLE) {
            getToolbar().setVisibility(View.GONE);
        }
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_my_splash, null);
    }


    /**
     * 继承 CountDownTimer 防范
     * <p>
     * 重写 父类的方法 onTick() 、 onFinish()
     */
    class MyCountDownTimer extends CountDownTimer {
        /**
         * @param millisInFuture    表示以毫秒为单位 倒计时的总数
         *                          <p>
         *                          例如 millisInFuture=1000 表示1秒
         * @param countDownInterval 表示 间隔 多少微秒 调用一次 onTick 方法
         *                          <p>
         *                          例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            finish();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (mStvBottomTime != null) {
                mStvBottomTime.setText(millisUntilFinished / 1000 + "s");
            }

        }
    }

}
